/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.apidoc.utility;

import com.hyts.apidoc.base.BaseApiParser;
import com.hyts.apidoc.config.ParseDocType;
import com.hyts.apidoc.parser.AnnotationApiParser;
import com.hyts.apidoc.parser.JavaDocApiParser;
import com.hyts.apidoc.parser.SwaggerApiParser;

/** 
 * @title ParserUtils.java  
 * @package com.hyts.apidoc.utility  
 * @description 解析操作工具类服务  
 * @see TODO
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年4月23日 
 * @version V1.0 
 */
public final class ParserFactory {
	
	/**  
	 * <p>{字段的描述}</p> 
	 * @fieldName docType
	 * @fieldType ParseDocType
	 */ 
	static ParseDocType docType = ParseDocType.JAVADOC;
	
	/**  
	 * <p>{字段的描述}</p> 
	 * @fieldName parserLocal
	 * @fieldType ThreadLocal<BaseApiParser>
	 */ 
	public static ThreadLocal<BaseApiParser> parserLocal = new ThreadLocal<BaseApiParser>() {

		/* (非 Javadoc)
		 * <b>TODO(方法功能的描述)-  initialValue </b>
		 * <p>TODO(这里用一句话描述这个方法的作用)</p>
		 * @return
		 * @see java.lang.ThreadLocal#initialValue()  
		 * @exception
		 */ 
		@Override
		protected BaseApiParser initialValue() {
			this.set(newInstance()); 
			return this.get();
		}
	};
	
	/**  
	 * <b>TODO(方法功能的描述)</b>
	 * <p>TODO(这里用一句话描述这个方法的作用)</p>
	 * @return
	 * @exception
	 */ 
	public static boolean clearParserSubject() {
		 try {
			parserLocal.set(null);
			parserLocal.remove();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		 return true;
	}
	
	/**  
	 * <b>getInstance</b>
	 * <p>枚举方案生成工厂类</p>
	 * @param docType
	 * @return
	 * @exception
	 */ 
	private static BaseApiParser newInstance() {
		switch(docType) {
			case APIDOC:
				 return new AnnotationApiParser();
			case JAVADOC:
				 return new JavaDocApiParser();
			case SWAGGER:
				 return new SwaggerApiParser();
			default:
				 return new AnnotationApiParser();
		}
	}
	
	/**  
	 * <b>getInstance</b>
	 * <p>获取实例化对象</p>
	 * @return
	 * @exception
	 */ 
	public static BaseApiParser getInstance() {
		return parserLocal.get();
	}

	/**  
	 * @name docType's setter method
	 * @param docType
	 * @return void
	 */
	public static void setDocType(ParseDocType docType) {
		ParserFactory.docType = docType;
	}

	/**  
	 * @name docType's getter method
	 * @param none
	 * @return docType
	 */
	public static ParseDocType getDocType() {
		return docType;
	}
	
	
	public static void main(String[] args) {
		/*System.out.println(ParserFactory.getInstance());
		Thread newThread = new Thread(()->{
			ParserFactory.setDocType(ParseDocType.SWAGGER);
			System.out.println(ParserFactory.getInstance());
		});
		newThread.start();
		try {
			newThread.join(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
	
}
